package com.jiuzhe.commercial.ui.house.editor;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jiangyy.easydialog.SingleChoiceDialog;
import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.bean.HouseListBean;
import com.jiuzhe.commercial.bean.HouseStayListBean;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient;
import com.jiuzhe.commercial.ui.house.AddPhotoActivity;
import com.jiuzhe.commercial.utils.RxBus;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;
import com.jiuzhe.commercial.view.GlideRoundTransform;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by huanghao on 2018/6/7.
 */

public class EditorHomeStayActivity extends BaseActivity {
    @BindView(R.id.tv_location)
    RelativeLayout tvLocation;
    @BindView(R.id.tv_assort)
    RelativeLayout tvAssort;
    @BindView(R.id.tv_roomPrice)
    RelativeLayout tvRoomPrice;
    @BindView(R.id.tv_state)
    RelativeLayout tvState;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.tv_mlocation)
    TextView tvMlocation;
    @BindView(R.id.tv_mAssort)
    TextView tvMAssort;
    @BindView(R.id.tv_mRoomPrice)
    TextView tvMRoomPrice;
    @BindView(R.id.tv_mRoomBond)
    TextView tvMRoomBond;
    @BindView(R.id.tv_mState)
    TextView tvMState;
    @BindView(R.id.tv_photoSize)
    TextView tvPhotoSize;
    private Subscription mSubscription;
    private HouseStayListBean houseStayListBean;
    private String skuId;
    private List<String> mImgs;
    private String skuName;
    private List<HouseStayListBean.DatePriceListBean> datePriceList;
    private HouseListBean houseListBean;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_editor_homestay);
        houseStayListBean = new HouseStayListBean();
        houseListBean = new HouseListBean();
        skuId = getIntent().getStringExtra("skuid");
        SPUtil.put(this, "skuId", skuId);
        setTitle("修改房源");
        initData();
    }

    private void initData() {
        mSubscription = RxBus.getInstance().toObserverable(HouseStayListBean.class)
                .subscribe(new Action1<HouseStayListBean>() {
                    @Override
                    public void call(HouseStayListBean bean) {
                        loadData();
                    }
                });
    }

    @Override
    protected void loadData() {
        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomeStayActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeStayActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUDETAIL.substring(Constants.SKUDETAIL.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", SPUtil.getString(this, "skuId"));
        HttpClient.post(this, Constants.SKUDETAIL, map, new CallBack<HouseStayListBean>() {
            @Override
            public void onSuccess(HouseStayListBean result) {
                Constants.HOUSESTAYLISTBEAN = result;
                // skuName = result.getSkuName();
                mImgs = result.getImgs();
                houseStayListBean.setImgs(mImgs);
                tvMlocation.setText(result.getAddress() + result.getRoomNo());
                String type = result.getRoomType();
                if (("0").equals(type)){
                    tvMAssort.setText("公寓型，" + result.getBedroomNum() + "间卧室，" + result.getBedNum() + "张床，" + result.getToiletNum() + "个洗手间" );
                }else if (("1").equals(type)){
                    tvMAssort.setText("酒店型，" + result.getBedroomNum() + "间卧室，"  + result.getBedNum() + "张床，" + result.getToiletNum() + "个洗手间" );
                }else if (("2").equals(type)){
                    tvMAssort.setText("住宅型，" + result.getBedroomNum() + "间卧室，" + result.getBedNum() + "张床，" + result.getToiletNum() + "个洗手间");
                }

                tvMRoomPrice.setText(result.getDefPrice() + "元/每晚");
              //  datePriceList = result.getDatePriceList();
              //  tvMRoomBond.setText(result.getRoomBond() + "元/每晚");
                if (1 == (result.getDefStatus())) {
                    tvMState.setText("已上架");
                }else {
                    tvMState.setText("已下架");
                }
                if (mImgs != null && mImgs.size() > 0) {
                    tvPhotoSize.setText(mImgs.size() + "张照片");
                    RequestOptions options = new RequestOptions()
                            .placeholder(R.mipmap.image)
                            .error(R.mipmap.image)
                            .transform(new GlideRoundTransform(EditorHomeStayActivity.this, 5))
                            .priority(Priority.HIGH)
                            .diskCacheStrategy(DiskCacheStrategy.NONE);

                    Glide.with(EditorHomeStayActivity.this)
                            .load(mImgs.get(0))
                            .apply(options)
                            .into(img);
                }
            }
        });
    }


    @OnClick({R.id.tv_location, R.id.tv_assort, R.id.tv_roomPrice, R.id.tv_state, R.id.tv_roomBond, R.id.img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_location:  //位置
                UIUtils.startActivity(EditorHomeStayMapLocationActivity.class);
                break;
            case R.id.tv_assort:
                UIUtils.startActivity(EditorHomeAssortActivity.class);
                break;
            case R.id.tv_roomPrice:
                UIUtils.startActivity(EditorHomePriceActivity.class);
                break;
            case R.id.tv_state:
                setRoomState();
                //  UIUtils.startActivity(EditorHomeState.class);
                break;
            case R.id.tv_roomBond:
                UIUtils.startActivity(EditorHomeBondActivity.class);
                break;
            case R.id.img:
                Intent intent = new Intent(this,AddPhotoActivity.class);
                intent.putExtra("room",houseStayListBean);
                startActivity(intent);
                break;
        }
    }

    private void setRoomState() {
        new SingleChoiceDialog.Builder(this).setTitle("提示")
                .addList(new String[]{"可预定", "下架", "删除"})
                .setOnItemClickListener(new SingleChoiceDialog.OnItemClickListener() {
                    @Override
                    public void OnItemClick(String title, int position) {
                        if ("可预定".equals(title)) {
                            int state = 1;
                            alterHomeStayState(state);
                        } else if ("下架".equals(title)) {
                            int state = 5;
                            alterHomeStayState(state);
                        } else if ("删除".equals(title)) {
                            roomDelete();
                        }
                    }
                }).show();
    }

    /**
     * 修改房间状态
     */
    private void alterHomeStayState(int state) {
        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomeStayActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeStayActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUUPDATING.substring(Constants.SKUUPDATING.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", Constants.HOUSESTAYLISTBEAN.getId());

        map.put("roomStatus", state + "");

        HttpClient.post(this, Constants.SKUUPDATING, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                UIUtils.showToastSafe("修改房间状态成功");
                RxBus.getInstance().post(houseListBean);
                finish();
            }
        });
    }

    /**
     * 删除房源
     */
    private void roomDelete() {
        HashMap<String, String> map = new HashMap<>();
        String token = SPUtil.getString(EditorHomeStayActivity.this, Constants.TOKEN);
        String id = SPUtil.getString(EditorHomeStayActivity.this, Constants.ID);
        String autoSignStr = Constants.SKUDELETING.substring(Constants.SKUDELETING.lastIndexOf("0/") + 1) + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);
        map.put("token", id);
        map.put("sign", signStr);
        map.put("skuId", skuId);
        HttpClient.post(this, Constants.SKUDELETING, map, new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                UIUtils.showToastSafe("删除成功");
                RxBus.getInstance().post(houseListBean);
                finish();
            }
        });
    }

    // 返回
    public void back(View v) {
       RxBus.getInstance().post(houseListBean);
        finish();
    }

    @Override
    public void onBackPressed() {
        RxBus.getInstance().post(houseListBean);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSubscription != null) {
            if (!mSubscription.isUnsubscribed()) {
                mSubscription.unsubscribe();
            }
        }
    }
}
